package com.example.meituan.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.meituan.R;
import com.example.meituan.activity.bean.AdminBean;
import com.example.meituan.activity.bean.ShopCarBean;
import com.example.meituan.activity.bean.TalkBean;
import com.example.meituan.activity.user.adapter.MessageAdapter;
import com.example.meituan.activity.user.adapter.ShopCarAdapter;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.ShopCarDao;
import com.example.meituan.dao.TalkDao;

import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends AppCompatActivity implements  OnMessageItemClickListener{
    List<TalkBean> talkBeans;
    String value;
    AdminBean currentUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_message);
        currentUser= AdminDao.getUserInfo(MessageActivity.this);
        Intent intent = getIntent();
        value = intent.getStringExtra("ifUser");
        if(value.equals("user")){
            talkBeans = TalkDao.getAllTalksByUserId(currentUser.getUserId());
        }
        if(value.equals("business")){
            talkBeans = TalkDao.getAllTalksByBUsinessId(currentUser.getUserId());
        }


        androidx.recyclerview.widget.RecyclerView recyclerView=findViewById(R.id.message_recyclerView);
        if (talkBeans == null || talkBeans.isEmpty()) {
            Toast.makeText(this, "没有数据显示", Toast.LENGTH_SHORT).show();
            recyclerView.setAdapter(null);
        } else {
            MessageAdapter adapter = new MessageAdapter(talkBeans,  this,1); ;
            recyclerView.setAdapter(adapter);
        }

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        TextView back=findViewById(R.id.message_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void toMessageView(TalkBean talkBean) {
        String ifuser="";
        String business="";
        String user="";
        if(value.equals("user")){
            ifuser="user";
            user=currentUser.getUserId();
            business=talkBean.getShopId();
        }
        if(value.equals("business")){
            ifuser="bussiness";
            business=currentUser.getUserId();
            user=talkBean.getUserId();
        }
        Intent intent=new Intent(MessageActivity.this, MessageActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("ifuserId", ifuser);
        bundle.putString("businessId",business);
        bundle.putString("userId",user);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}